package top.humbleyuan.leet;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Author HumbleYuan
 * @Date 2019/12/10 15:47
 * @Des 数组
 */
public class Array {

    // 4. 寻找有序数组的中位数
    @Test
    public void array_4() {
        int[] a = new int[]{1};
        int[] b = new int[]{8, 9, 18, 20, 35};

        int[] ab = new int[a.length + b.length];
        System.arraycopy(a, 0, ab, 0, a.length);
        System.arraycopy(b, 0, ab, a.length, b.length);

        Arrays.sort(ab);
        float mid ;
        int l = a.length + b.length;
        if(l % 2 == 0) {
            mid = ((float) ab[l / 2] + (float) ab[l / 2 - 1]) / 2;
        }else {
            mid = ab[l/2];
        }
        System.out.println("中位数：" + mid);
    }
}